package com.gestionabsences.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author matt
 */
public class ToolsRequest {
    /**
    * Initialise la requête préparée basée sur la connexion passée en argument,
    * avec la requête SQL et les objets donnés.
    */
    public static PreparedStatement prepareRequest( Connection connexion, String sql, boolean returnGeneratedKeys, Object... objets ) throws SQLException {
	PreparedStatement preparedStatement;
	preparedStatement = connexion.prepareStatement( sql, returnGeneratedKeys ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS );
	for ( int i = 0; i < objets.length; i++ ) {
	    preparedStatement.setObject( i + 1, objets[i] );
	}
	return preparedStatement;
    }
    
    public static void close( ResultSet resultSet ) {
        if ( resultSet != null ) {
            try {
                resultSet.close();
            } catch ( SQLException ignore ) {}
        }
    }
    
    public static void close(PreparedStatement preparedStatement) {
	if ( preparedStatement != null ) {
	    try {
		preparedStatement.close();
	    } catch ( SQLException ignore ) {}
	}
    }
    
    public static void close(Connection connexion) {
	if ( connexion != null ) {
            try {
                connexion.close();
            } catch ( SQLException ignore ) {}
        }
    }
    
    public static void close(PreparedStatement preparedStatement, ResultSet resultSet) {
	close(resultSet);
	close(preparedStatement);
    }
    
    public static void close(Connection connexion, PreparedStatement preparedStatement, ResultSet resultSet) {
	close(resultSet);
	close(preparedStatement);
	close(connexion);
    }
}
